package ru.usu.gv.gui.actions;

import ru.usu.gv.core.GraphCanvas;
import ru.usu.gv.gui.routing.EdgeRouter;
import ru.usu.gv.uicomponents.UIEdge;
import ru.usu.gv.uicomponents.UIGraph;
import ru.usu.gv.uicomponents.UIVertex;

/**
 * @author spupyrev
 * 28.10.2009
 * 
 * Route edges around the nodes
 */
public class EdgeRoutingAction extends ActionBase
{
	private UIGraph uiGraph;

	public EdgeRoutingAction()
	{
		uiGraph = GraphCanvas.getInstance().getCurrentGraph();
	}

	@Override
	public void execute()
	{
		System.out.println("Edge routing...");

		projection2D();
		straightenEdges();
		routeEdges();
	}

	private void projection2D()
	{
		for (UIVertex uiVertex : uiGraph.getVertexes())
			uiVertex.getPosition().z = 0;
	}

	private void straightenEdges()
	{
		for (UIEdge uiEdge : uiGraph.getEdges())
			uiEdge.getViewItem().rectificate();
	}

	private void routeEdges()
	{
		for (UIEdge uiEdge : uiGraph.getEdges())
			if (uiEdge.getEdgeType().getId().equals("spline") || uiEdge.getEdgeType().getId().equals("splinetube"))
				new EdgeRouter(uiGraph, uiEdge).route();

	}

}
